package ntd.util.frame.mybatis.plus;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.c.Constant;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 字段填充器
 */
@Component
public class MybatisPlusObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        Date date = new Date();
        this.strictInsertFill(metaObject, "createdDate", Date.class, date);
        this.strictInsertFill(metaObject, "createdBy", String.class, getUserId());
        this.strictInsertFill(metaObject, "updatedDate", Date.class, date);
        this.strictInsertFill(metaObject, "updatedBy", String.class, getUserId());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        Date date = new Date();
        this.strictUpdateFill(metaObject, "updatedDate", Date.class, date);
        this.strictUpdateFill(metaObject, "updatedBy", String.class, getUserId());
    }

    /**
     * 获取userId 供该类使用
     * @return
     */
    private String getUserId() {
        return Constant.ID_DEFAULT;
    }
}
